package com.ezops.config;

import java.net.MalformedURLException;

import javax.sql.DataSource;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.transaction.PlatformTransactionManager;

import com.ezops.businessprocess.integration.usermanagement.EzopsUserEntityManagerFactory;
import com.ezops.poc.dao.UserDAO;

//@Configuration
public class TradeProcessingConfig {

	private final Logger log = LoggerFactory
			.getLogger(TradeProcessingConfig.class);

	@Autowired
	protected Environment environment;

	@Autowired
	protected UserDAO dao;
	@Bean
	public DataSource dataSource() {
		final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
		dsLookup.setResourceRef(true);
		DataSource dataSource = dsLookup.getDataSource("jdbc/EZOPSDB");
		return dataSource;
		// ds.setJndiName("java:comp/env/jdbc/EZOPSDBACTIVITI");
	}

	@Bean(name = "transactionManager")
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSource());
		return transactionManager;
	}

	@Bean(name = "processEngineFactoryBean")
	public ProcessEngineFactoryBean processEngineFactoryBean() throws MalformedURLException {
		ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
		factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
		return factoryBean;
	}

	@Bean(name = "processEngine")
	public ProcessEngine processEngine() {
		// Safe to call the getObject() on the @Bean annotated
		// processEngineFactoryBean(), will be
		// the fully initialized object instanced from the factory and will
		// NOT be created more than once
		try {
			return processEngineFactoryBean().getObject();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Bean(name = "processEngineConfiguration")
	public ProcessEngineConfigurationImpl processEngineConfiguration() throws MalformedURLException {
		SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
		processEngineConfiguration.setDataSource(dataSource());
		processEngineConfiguration.setDatabaseSchemaUpdate(environment
				.getProperty("engine.schema.update", "true"));
		processEngineConfiguration
				.setTransactionManager(annotationDrivenTransactionManager());
		processEngineConfiguration.setJobExecutorActivate(Boolean
				.valueOf(environment.getProperty("engine.activate.jobexecutor",
						"false")));

		Resource resource = new org.springframework.core.io.UrlResource(
				"file:/Users/vgaurav/EZOPS/Trade_Exception_Workflow.bpmn");
		Resource[] resources = {resource};
		processEngineConfiguration.setDeploymentResources(resources);
		EzopsUserEntityManagerFactory fac = new EzopsUserEntityManagerFactory();
		fac.setUserDao(dao);
		processEngineConfiguration.getCustomSessionFactories().add(fac);

		// <property name="deploymentResources"
		// value="file:/Users/vgaurav/EZOPS/Trade_Exception_Workflow.bpmn" />
		// <!-- <property name="camelBehaviorClass"
		// value="org.activiti.camel.impl.CamelBehaviorCamelBodyImpl" /> -->
		// <property name="customSessionFactories">
		// <list>
		// <bean
		// class="com.ezops.businessprocess.integration.usermanagement.EzopsUserEntityManagerFactory">
		// <property name="userDao" ref="userDao" />
		// </bean>
		// </list>
		// </property>

		return processEngineConfiguration;
	}

	@Bean
	public RepositoryService repositoryService() {
		return processEngine().getRepositoryService();
	}

	@Bean
	public RuntimeService runtimeService() {
		return processEngine().getRuntimeService();
	}

	@Bean
	public TaskService taskService() {
		return processEngine().getTaskService();
	}

	@Bean
	public HistoryService historyService() {
		return processEngine().getHistoryService();
	}

	@Bean
	public FormService formService() {
		return processEngine().getFormService();
	}

	@Bean
	public IdentityService identityService() {
		return processEngine().getIdentityService();
	}

	@Bean
	public ManagementService managementService() {
		return processEngine().getManagementService();
	}
	
	
}
